package com.ytjj.qmyx.users.utils;

/**
 *
 * @version 1.0
 *
 */

import com.alibaba.fastjson.JSONObject;
import com.tencent.ads.ApiContextConfig;
import com.tencent.ads.TencentAds;
import com.tencent.ads.exception.TencentAdsResponseException;
import com.tencent.ads.exception.TencentAdsSDKException;
import com.tencent.ads.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AddUserActions {
    /** YOUR ACCESS TOKEN */
//    public String ACCESS_TOKEN = "39ccff7d4207d6e21cfabb82abb9d577";

    /** YOUR ACCESS TOKEN */
    public String ACCESS_TOKEN = "515b515074ba53db8f36da05eaa9c3e8";

    /** TencentAds */
    public TencentAds tencentAds;

//    public Long accountId = 18006086L;
    public Long accountId = 19133918L;
    public UserActionsAddRequest data = new UserActionsAddRequest();
    public Long actionTime = null;
    public String hashImei = null;
    // public ActionType actionType = ActionType.ACTIVATE_APP;
    public String customAction = null;
//    public Long userActionSetId = 1111251162L;
    public Long userActionSetId = 1111385575L; //幸福汇小程序激活

    public void init(String openid, Long userActionSetId,ActionType actionType,String aid) {
        this.tencentAds = TencentAds.getInstance();
        this.tencentAds.init(
                new ApiContextConfig().accessToken(ACCESS_TOKEN).isDebug(true)); // debug==true 会打印请求详细信息
//        this.tencentAds.useSandbox(); // 默认使用沙箱环境，如果要请求线上，这里需要设为线上环境
         this.tencentAds.useProduction(); // 使用线上
        this.buildParams(openid,userActionSetId,actionType,aid);
    }

    public void buildParams(String openid, Long userActionSetId,ActionType actionType,String aid) {
        data.setAccountId(accountId);

        UserAction userAction = new UserAction();
        if(StringUtils.isNotBlank(aid)){
            Trace trace = new Trace();
            trace.setClickId(aid);
            userAction.setTrace(trace);
        }
        userAction.setActionTime(actionTime);
        ActionsUserId userId = new ActionsUserId();
        userId.setHashImei(hashImei);
        // 小程序
        userId.setWechatOpenid(openid);
        userId.setWechatAppId("wxc2d6e3dff52c8c87");
        userAction.setUserId(userId);
        userAction.setActionType(actionType);
        userAction.setCustomAction(customAction);
        List<UserAction> actions = new ArrayList<>();
        actions.add(userAction);
        data.setActions(actions);

        data.setUserActionSetId(userActionSetId);
        log.info("广点通上报数据请求参数：{}", JSONObject.toJSONString(data));
    }

    public UserActionsAddResponse addUserActions() throws Exception {
        UserActionsAddResponse response = tencentAds.userActions().userActionsAdd(data);
        return response;
    }

    public static void main(String[] args) {
        try {
            AddUserActions addUserActions = new AddUserActions();
            // addUserActions.init("o-aXM4jx_YX7djRgzs6zNgBDiuLY",l);
            UserActionsAddResponse response = addUserActions.addUserActions();
        } catch (TencentAdsResponseException e) {
            e.printStackTrace();
        } catch (TencentAdsSDKException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
